package net.hubt.itc.ui;



import net.hubt.itc.data.LookupCommand;
import net.hubt.itc.data.StudentTranscriptList;
import net.hubt.itc.data.TranscriptData;
import net.hubt.itc.data.TranscriptXMLParser;
import net.hubt.itc.io.net.HttpUtils;
import net.hubt.itc.ui.utils.CTable;
import net.hubt.itc.util.LayoutUtils;
import net.hubt.itc.util.StringUtils;
import net.hubt.itc.util.XMLDocument;
import android.app.AlertDialog;
import android.app.ProgressDialog;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import android.view.Gravity;

public class TranscriptActivity extends BaseActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onStart() {
		super.onStart();
		executeGetTranscriptTask();
	}

	private void executeGetTranscriptTask() {
		prepareParamsForTask();
		new GetTranscriptTask().execute(baseUrl + "/xml.php", "msv", userId,
				"type", LookupCommand.CMD_TRANSCRIPT + "");
	}

	class GetTranscriptTask extends AsyncTask<String, Integer, String> {
		ProgressDialog dlgProgress;
		
		@Override
		protected void onPreExecute() {		
			super.onPreExecute();
			dlgProgress=ProgressDialog.show(TranscriptActivity.this, getString(R.string.app_global_load_title),
					getString(R.string.app_global_load_message),true);						
		}
		
		@Override
		protected String doInBackground(String... addresses) {
			try {
				//connect to network, get xml data
				String transcriptXmlData = HttpUtils.urlContentPost(addresses);
				
				return transcriptXmlData;
			} catch (Exception ex) {
				//network error
				return "100";
//				return ex.getMessage();
			}
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			
			//Log.e("onProgressUpdate", "onProgressUpdate");
		}

		@Override
		protected void onPostExecute(String transcriptXmlData) {
			// tvContent.setText(transcriptXmlData);
			// xmlContent=result;
			if(transcriptXmlData.equalsIgnoreCase("100")){			
				dlgProgress.dismiss();
				AlertDialog dialog=new AlertDialog.Builder(TranscriptActivity.this).create();
				dialog.setTitle(getString(R.string.ui_trans_desc_short));
				//dialog.setMessage(getString(R.string.ui_trans_error_msg));			
				dialog.setMessage(getString(R.string.ui_common_network_error_msg));
				dialog.setIcon(android.R.drawable.ic_dialog_info);
				dialog.setButton("OK", new DialogInterface.OnClickListener() {				
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				}); 
					
				dialog.show();
			}
			else{
				processTranscriptData(transcriptXmlData);
				dlgProgress.dismiss();
			}
		}
	}

	protected void processTranscriptData(String xmlData) {
		XMLDocument xmlDoc = new XMLDocument();
		//Document document = xmlDoc.getDocument(xmlData);

		TranscriptXMLParser t = new TranscriptXMLParser();
		t.setRoot(xmlDoc.getRootNode(xmlData));

		StudentTranscriptList studentTranscript = t.parseXmlData();
		if(studentTranscript!=null){
			CTable transcriptTableUI = buildTranscriptTableUI(this,
					studentTranscript);
			setContentView(LayoutUtils.createLinearScrollableTableLayout(this,
					transcriptTableUI,
					getString(R.string.ui_trans_desc)
							+ studentTranscript.getTenSinhVien()));
		}
		else{
			AlertDialog dialog=new AlertDialog.Builder(TranscriptActivity.this).create();
			dialog.setTitle(getString(R.string.ui_trans_desc_short));
//			dialog.setMessage(String.format(getString(R.string.ui_data_notfound_msg), 
//								getString(R.string.ui_trans_desc_short), userId));
			dialog.setMessage(getString(R.string.ui_trans_not_found_msg));
			dialog.setIcon(android.R.drawable.ic_dialog_info);
			dialog.setButton("OK", new DialogInterface.OnClickListener() {				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startActivity(new Intent(TranscriptActivity.this, MainUI.class));
				}
			}); 
				
			dialog.show();				
		}

	}

	protected CTable buildTranscriptTableUI(Context context,
			StudentTranscriptList studentTranscript) {
		CTable transcriptTable = new CTable(context);
		transcriptTable.stretchColumn(0, true);
		transcriptTable.stretchColumn(1, true);
		if (studentTranscript != null) {
			int cnt = studentTranscript.countTranscript();
			for (int i = 0; i < cnt; i++) {
				TranscriptData transObject = studentTranscript
						.getTranscript(i);			
				transcriptTable.addRow(transcriptTable.makeColSpanRow(new int[]{Gravity.RIGHT},2,new boolean[]{true},transObject.getTenMonHoc()));
				transcriptTable.addRow(transcriptTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_trans_idky),
						transObject.getIdKy()));
				transcriptTable.addRow(transcriptTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_trans_dvht),
						transObject.getDvht()));
				if(!StringUtils.isEmpty(transObject.getDiemTB1()))
				transcriptTable.addRow(transcriptTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_trans_tb1),
						transObject.getDiemTB1()));
				if(!StringUtils.isEmpty(transObject.getDiemTB2()))
				transcriptTable.addRow(transcriptTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_trans_tb2),
						transObject.getDiemTB2()));
				
				//khong hien thi truong KHONG TINH DIEM
				/*
				transcriptTable.addRow(transcriptTable.makeRow(new int[] {
						Gravity.RIGHT, Gravity.RIGHT }, new boolean[]{false,true}, getString(R.string.ui_trans_khongtd),
						transObject.getKhongTd()));
						*/							
			}
		}
		return transcriptTable;
	}
}
